$(function(){
    $('.foldicon i').click(function(){
        $(this).parent().toggleClass("rotate");
        $(this).parents('div.lotterytype').children(".lotteryhot").slideToggle("slow");
    })
    $('.tcgmenutoggle').click(function(){
        if(!$(this).hasClass("back")){
            $(this).parents('div.tcgleft').css({ "marginLeft": "-15.4%"});
            $('.tcgright').width('100%');
            $(this).toggleClass('back');
        }else{
            $(this).parents('div.tcgleft').css({ "marginLeft": "0%"});
            $('.tcgright').width('84.5%');
            $(this).toggleClass('back');
        }   
    })
})
var app=new Vue({
    el:'.tcgcontainer',
    computed: {
        exchangenum: {//兑换数量
            get() {
                if (!isNaN(Number(this.exchange.exchangenum))) {
                    return this.exchange.exchangenum
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0) {
                        this.exchange.exchangenum = 0
                    } else {
                        this.exchange.exchangenum = parseFloat(v)
                    }
                    if (parseFloat(Number(v)) > parseFloat(this.userinfo.banlance)) {
                        this.exchange.exchangenum = parseInt(this.userinfo.banlance)
                    }
                    this.exchange.exchangenum = Math.floor(this.exchange.exchangenum * 100) / 100
                    let computedresult = parseFloat(Number(this.exchange.exchangenum)) * this.exchange.agratenum
                    if (!isNaN(Number(computedresult))) {
                        this.exchange.exchangeagcoin = computedresult
                        if (this.exchange.exchangeagcoin > 3000) {
                            this.exchange.disexchangenum = true
                            let lang = this.getCookie("lang");
                            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                            var b = '<div class="maskLayer"></div>'
                            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                                this.exchange.disexchangenum = false
                                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                                document.querySelector('.container').removeChild(c)
                                document.querySelector('.container').removeChild(d)
                            }
                            if (lang == "en_US") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "The amount is greater than 3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "The maximum redemption limit for TCG points is 3000"
                            } else if (lang == "zh_TW") {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "額度大於3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "TCG分最大兌換額度為3000"
                            } else {
                                document.getElementsByClassName('errorMessageTop')[0].innerHTML = "额度大于3000"
                                document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "TCG分最大兑换额度为3000"
                            }
                        }
                    } else {
                        this.exchange.exchangeagcoin = 0
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        },
        cashoutnum: { //兑出数量
            get() {
                if (!isNaN(Number(this.cashout.exchangenum))) {
                    if (parseFloat(this.cashout.exchangenum) >= 0) {
                        return this.cashout.exchangenum
                    } else {
                        return 0
                    }
                } else {
                    return 0
                }
            },
            set(v) {
                if (!isNaN(Number(v))) {
                    if (parseFloat(Number(v)) < 0) {
                        this.cashout.exchangenum = 0
                    } else {
                        this.cashout.exchangenum = parseInt(v)
                    }
                    if (parseFloat(Number(v)) > parseFloat(this.userinfo.myagbanlance)) {
                        this.cashout.exchangenum = parseInt(this.userinfo.myagbanlance)
                    }
                    this.cashout.exchangenum = Math.floor(this.cashout.exchangenum * 100) / 100
                    let computedresult = parseFloat(Number(this.cashout.exchangenum)) / parseFloat(this.exchange.agratenum)
                    computedresult = parseFloat(computedresult.toFixed(8))
                    if (!isNaN(Number(computedresult))) {
                        this.cashout.exchangeagcoin = computedresult
                    } else {
                        this.cashout.exchangeagcoin = 0
                    }
                } else {
                    this.cashout.exchangeagcoin = 0
                }
            }
        }
    },
    data(){
        return {
            istcgdialog:false, //是否展示兑换dialog
            showtransforminag: true,//兑换AG分
            showtransformout: false,//兑出AG分
            exchange: { //兑换配置项
                defaultcoin: 'HPS', //默认币种
                isshowcoinarr: false, //是否展示下拉选
                exchangenum: '0', //兑换数量
                disexchangenum: false,//禁用兑换数量
                exchangeagcoin: '0',//兑换后数量
                agratenum: 5, //AG兑换比例
                disabledbtn: false //禁用兑换点击按钮
            },
            cashout: { //兑出配置项
                exchangenum: '0', //兑换数量
                exchangeagcoin: '0',//兑换后币种数量
                disabledbtn: false //禁用兑出点击按钮
            },
            userinfo: { //用户信息
                banlance: '',//用户余额
                myagbanlance: 0,//我的ag积分余额
                lottoName:'Lobby', //彩种名称code 默认大厅
                viewName:'Lobby', //彩票页面，大厅页
                gameurl:'' //TCG彩票地址
            },
            togglecoin:{ //用户切换币种
                isshowcoinarr:false //是否展示下拉选
            },
            theuser: "",
            information: "",
            loading: false
        }
    },
    created() {
        this.getagBalance()
        this.getagDict(this.exchange.defaultcoin)
    },
    methods:{
        getagBalance() {//获取当前用户TCG分
            let params = {
                coin: this.exchange.defaultcoin
            }
            axios.post('/tcgBalance', params).then((res) => {
                if (res.data.code == 200) {
                    if(res.data.data.status==0){
                        this.userinfo.myagbanlance = res.data.data.balance
                        this.getlaunchGame()
                    }
                }
            })
        },
        getagDict(coinname) { //选择币种获取兑换比例
            if (!coinname) {
                return
            }
            axios.get(`/ag/dict/${coinname}`, {}).then((res) => {
                this.exchange.agratenum = parseFloat(res.data.data.dictValue)
            })
        },
        changeexchangecoin(cointype, banlance) {//用户点击更换币种
            this.exchange.defaultcoin = cointype
            this.exchange.isshowcoinarr = false
            this.togglecoin.isshowcoinarr = false
            this.userinfo.banlance = banlance
            this.getagDict(cointype)
            this.getagBalance()
        },
        changetransform(keyname) { //用户点击兑换、兑出AG分栏目
            if (!keyname) {
                return
            }
            this.$data[keyname] = true
            this.getagDict(this.exchange.defaultcoin) //根据币种换算兑换比例
            if (keyname == 'showtransforminag') { //兑换AG分
                this.showtransformout = false
            } else if (keyname == 'showtransformout') {//兑出AG分
                this.showtransforminag = false
            }
        },
        confirmexchange() { //确定兑换TCG积分
            //为空、NaN、
            if (
                this.exchange.exchangenum == '' || isNaN(Number(this.exchange.exchangenum)) || this.exchange.exchangenum == 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌換提示',
                        title2:'請輸入兌換數量大於零'
                    },
                    zh_CN:{
                        title1:'兑换提示',
                        title2:'请输入兑换数量大于零'
                    },
                    en_US:{
                        title1:'Exchange tips',
                        title2:'Redemption quantity is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            if (parseFloat(Number(this.exchange.exchangenum)) > parseFloat(this.userinfo.banlance)) { //兑换数量>用户余额
                let lang = this.getCookie("lang");
                var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
                var b = '<div class="maskLayer"></div>'
                document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
                document.getElementsByClassName("closeinfo")[0].onclick = () => {
                    var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                    var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                    document.querySelector('.container').removeChild(c)
                    document.querySelector('.container').removeChild(d)
                }
                if (lang == "en_US") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "Lack of balance"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "Insufficient account balance"
                } else if (lang == "zh_TW") {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "餘額不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "賬戶餘額不足！"
                } else {
                    document.getElementsByClassName('errorMessageTop')[0].innerHTML = "余额不足"
                    document.getElementsByClassName('errorMessageBottom')[0].innerHTML = "账户余额不足！"
                }
                return
            }
            this.exchange.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.exchange.exchangenum),
                type:1
            }
            axios.post('/tcgTransfer', params).then((res) => {
                this.exchange.disabledbtn = false
                if (res.data.code == 200) {
                    if(res.data.data.status==0){
                        let dialogopt={
                            zh_TW:{
                                title1:'兌換成功',
                                title2:'TCG積分兌換成功！'
                            },
                            zh_CN:{
                                title1:'兑换成功',
                                title2:'TCG积分兑换成功！'
                            },
                            en_US:{
                                title1:'Successfully redeemed',
                                title2:'Successfully converted the TCG points.'
                            }
                        }
                        this.opendialog(dialogopt)
                        this.getagBalance()
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'請稍後重試!'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                    this.istcgdialog=false
                }else{
                    this.istcgdialog=false
                    if(res.data.error&&res.data.error.status){
                        if(res.data.error.status==11){
                            let dialogopt={
                                zh_TW:{
                                    title1:'提示',
                                    title2:'提現餘額不足!'
                                },
                                zh_CN:{
                                    title1:'提示',
                                    title2:'提现余额不足!'
                                },
                                en_US:{
                                    title1:'Exchange tips',
                                    title2:'Insufficient cash withdrawal.'
                                }
                            }
                            this.opendialog(dialogopt)
                        }else if(res.data.error.status==12){
                            let dialogopt={
                                zh_TW:{
                                    title1:'提示',
                                    title2:'交易序號已經存在!'
                                },
                                zh_CN:{
                                    title1:'提示',
                                    title2:'交易序号已经存在!'
                                },
                                en_US:{
                                    title1:'Exchange tips',
                                    title2:'Transaction serial number already exists.'
                                }
                            }
                            this.opendialog(dialogopt)
                        }else if(res.data.error.status==16){
                            let dialogopt={
                                zh_TW:{
                                    title1:'提示',
                                    title2:'信用額度不足，請聯繫客服!'
                                },
                                zh_CN:{
                                    title1:'提示',
                                    title2:'信用额度不足，请联系客服!'
                                },
                                en_US:{
                                    title1:'Exchange tips',
                                    title2:'Insufficient credit limit, please contact customer service.'
                                }
                            }
                            this.opendialog(dialogopt)
                        }else{
                            let dialogopt={
                                zh_TW:{
                                    title1:'提示',
                                    title2:'處理失敗，請聯繫客服!'
                                },
                                zh_CN:{
                                    title1:'提示',
                                    title2:'处理失败，请联系客服!'
                                },
                                en_US:{
                                    title1:'Exchange tips',
                                    title2:'Processing failed, please contact customer service!.'
                                }
                            }
                            this.opendialog(dialogopt)
                        }
                    }
                }
            })
        },
        confircashout(){ //确定兑回TCG积分
            if (
                localStorage.getItem('ID') == '' ||
                localStorage.getItem('ID') == ''
            ) {
                location.href = '/singin'
                return
            }
            if (
                this.userinfo.myagbanlance == '' ||
                this.cashout.exchangenum == 0 ||
                this.cashout.exchangenum == '' ||
                isNaN(Number(this.userinfo.myagbanlance)) ||
                parseFloat(this.userinfo.myagbanlance) <= 0
            ) {
                let dialogopt={
                    zh_TW:{
                        title1:'兌回提示',
                        title2:'請輸入兌回數量大於零'
                    },
                    zh_CN:{
                        title1:'兑回提示',
                        title2:'请输入兑回数量大于零'
                    },
                    en_US:{
                        title1:'Redemption tips',
                        title2:'The amount of redemption is greater than zero.'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            this.cashout.disabledbtn = true
            let params = {
                coin: this.exchange.defaultcoin,
                amount: parseFloat(this.cashout.exchangenum),
                type: 2
            }
            axios.post('/tcgTransfer', params).then((res) => {
                this.cashout.disabledbtn = false
                if (res.data.code == 200) {
                    if(res.data.data.status==0){
                        this.getagBalance()
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'請稍後重試!'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                }
                this.istcgdialog=false
            })
        },
        getlaunchGame(){ //获取游戏大厅
            let params={
                coin:this.exchange.defaultcoin,
                lottoName:this.userinfo.lottoName,
                pageName:this.userinfo.viewName,
                lottoType:'Elott_Traditional'
            }
            this.loading=true
            axios.post('/tcgGameurl',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data.status==0){
                        this.userinfo.gameurl=`http://tcg.play.top/${res.data.data.game_url}`
                    }
                }
                this.loading=false
            })
        },
        opendialog(obj) { //打开遮罩层
            let lang = this.getCookie("lang");
            var a = '<div class="errorMessage"><div class="errorMessageTop" v-html="theuser"></div> <div class="errorMessageBottom" v-html="information"></div><img src="/image/close.png" alt="" class="closeinfo"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', a)
            var b = '<div class="maskLayer"></div>'
            document.querySelector('.container').insertAdjacentHTML('afterBegin', b)
            document.getElementsByClassName("closeinfo")[0].onclick = () => {
                var c = document.getElementsByClassName('maskLayer')[0]  //移除元素
                var d = document.getElementsByClassName('errorMessage')[0] //移除元素
                document.querySelector('.container').removeChild(c)
                document.querySelector('.container').removeChild(d)
            }
            document.getElementsByClassName('errorMessageTop')[0].innerHTML = obj[lang].title1
            document.getElementsByClassName('errorMessageBottom')[0].innerHTML = obj[lang].title2
        },
        getCookie(cookiename) { //获取cookie
            var arr, reg = new RegExp("(^| )" + cookiename + "=([^;]*)(;|$)");
            if (arr = document.cookie.match(reg)) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        },
        toggletcgurl(lottoName,viewName){ //左侧菜单栏切换彩票游戏
            if(!lottoName){
                return 
            }
            this.userinfo.lottoName=lottoName
            this.userinfo.viewName=viewName
            this.getlaunchGame()
        },
        openistcgdialog(){ //打开遮罩层
            this.istcgdialog=true
            this.togglecoin.isshowcoinarr = false
        }
    }
})